/*
Project name :            _33lambda_func
Last modified Date:        2022年5月5日21点04分
Last Version:            V1.0
Descriptions:            C++ 的 lambda用法
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>
const long Size1 = 39L;
const long Size2 = 10 * Size1;
const long Size3 = 10 * Size2;
const long Size4 = 10 * Size3;
bool f3(int x) { return x % 3 == 0; }
bool f13(int x) { return x % 13 == 0; }
int main()
{
    using std::cout;
    using std::endl;
    std::vector<int> numbers(Size1);
    std::srand(std::time(0));
    std::generate(numbers.begin(), numbers.end(), std::rand);
    // using function pointers
    cout << "使用函数指针*********************************************************" << endl;
    cout << "Sample size = " << Size1 << '\n';
    long count3 = std::count_if(numbers.begin(), numbers.end(), f3);
    cout << "Count of numbers divisible by 3: " << count3 << '\n';
    long count13 = std::count_if(numbers.begin(), numbers.end(), f13);
    cout << "Count of numbers divisible by 13: " << count13 << "\n\n";

    // increase number of numbers
    numbers.resize(Size2);
    std::generate(numbers.begin(), numbers.end(), std::rand);
    cout << "使用伪函数***********************************************************" << endl;
    cout << "Sample size = " << Size2 << '\n';
    // using a functor
    class f_mod
    {
    private:
        int dv;
    public:
        f_mod(int d = 1) : dv(d) {}
        bool operator()(int x) { return x % dv == 0; }
    };
    count3 = std::count_if(numbers.begin(), numbers.end(), f_mod(3));
    cout << "Count of numbers divisible by 3: " << count3 << '\n';
    count13 = std::count_if(numbers.begin(), numbers.end(), f_mod(13));
    cout << "Count of numbers divisible by 13: " << count13 << "\n\n";

    // increase number of numbers again
    numbers.resize(Size3);
    std::generate(numbers.begin(), numbers.end(), std::rand);
    cout << "lambda函数简单使用**************************************************" << endl;
    cout << "Sample size = " << Size3 << '\n';
    // using lambdas
    count3 = std::count_if(numbers.begin(), numbers.end(),
        [](int x) {return x % 3 == 0; });
    cout << "Count of numbers divisible by 3: " << count3 << '\n';
    count13 = std::count_if(numbers.begin(), numbers.end(),
        [](int x) {return x % 13 == 0; });
    cout << "Count of numbers divisible by 13: " << count13 << "\n\n";

    // increase number of numbers again
    numbers.resize(Size4);
    std::generate(numbers.begin(), numbers.end(), std::rand);
    std::generate(numbers.begin(), numbers.end(), std::rand);
    cout << "Sample size = " << Size4 << '\n';
    // using lambdas
    count3 = std::count_if(numbers.begin(), numbers.end(),
        [](int x) {return x % 3 == 0; });
    cout << "Count of numbers divisible by 3: " << count3 << '\n';
    count13 = 0;
    std::for_each(numbers.begin(), numbers.end(),
        [&count13](int x) {count13 += x % 13 == 0; });
    cout << "Count of numbers divisible by 13: " << count13 << "\n\n";
    // using a single lambda
    cout << "可访问作用域内任何变量的lambda***************************************" << endl;
    count3 = count13 = 0;
    cout << "Sample size = " << Size4 << '\n';
    std::for_each(numbers.begin(), numbers.end(),
        [&](int x) {count3 += x % 3 == 0; count13 += x % 13 == 0; });
    cout << "Count of numbers divisible by 3: " << count3 << '\n';
    cout << "Count of numbers divisible by 13: " << count13 << "\n\n";
    cout << "返回类型后置lambda and 有名字的lambda函数****************************" << endl;
    auto mod3 = [](double x)->double {int y = x; return x - y; };// return type is double
    double result = mod3(9.99);
    cout << "result = " << result << endl;
    return 0;
}